package com.ebelter.btcomlib.models.bluetooth.products.ear_temperature;

import android.text.TextUtils;

import com.ebelter.btcomlib.models.bluetooth.products.ear_temperature.bean.EarTemMesureResult;
import com.ebelter.btcomlib.models.bluetooth.products.ear_temperature.interfaces.EarTemperatureMesureResult;
import com.ebelter.btcomlib.utils.BaseHandle;
import com.ebelter.btcomlib.utils.BytesUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;


public class EarTemperatureBytesAnalysis extends BaseHandle{

    private static final String TAG = "EarTemperatureBytesAnalysis";

    private EarTemperatureMesureResult earTemperatureMesureResult;


    public void resultAnalysis(byte[] data) {
        if (earTemperatureMesureResult==null){
            return;
        }
        if(data==null||data.length<2){
            return;
        }
        LogUtils.i(TAG,"resultAnalysis----"+ BytesUtils.bytes2HexStr(data));
        int cmdCode = data[0] & 0xff;
        switch (cmdCode) {
            case 0xff: //接收到当前测量结果
                doResult(data);
                break;
            case 0xee: //错误码
                doError(data);
                break;
            case 0xf0: //关机
                doPoweroff(data);
                break;
        }
    }


    private String mesurTime;
    private long mesurTimeLong;
    /**
     * 收到测试结果
     */
    private void doResult(byte[] data) {
        if (data.length<3){
            return;
        }

        long measureTime = System.currentTimeMillis();
        String mt= TimeUtils.formatTime1(measureTime);
        if (TextUtils.equals(mesurTime,mt)||measureTime-mesurTimeLong<2000) {
            LogUtils.i(TAG,"体温测量时间一致 被reture");
            return;
        }

//        int high = data[2]&0xff;
//        int low = data[3]&0xff;
        mesurTime=mt;
        mesurTimeLong=measureTime;
        final EarTemMesureResult result=new EarTemMesureResult(measureTime,data[1],get2ByteValue(data[2], data[3])/10.0f);

        post(new Runnable() {
            @Override
            public void run() {
                earTemperatureMesureResult.mesureResult(result);
            }
        });

    }

    private int get2ByteValue(byte high, byte low) {
        return ((high & 0xff) << 8) | (low & 0xff);
    }

    /**
     * 错误码
     */
    private void doError(byte[] data) {
        final int errCmd = data[1] & 0xff;
        LogUtils.i(TAG, "-------doError--------errCmd = " + errCmd);
        post(new Runnable() {
            @Override
            public void run() {
                earTemperatureMesureResult.error(errCmd);
            }
        });
       ;
    }

    /**
     * 关机
     */
    private void doPoweroff(byte[] data) {
        int poweroff = data[1] & 0xff;
        if (poweroff == 0xff) {
            post(new Runnable() {
                @Override
                public void run() {
                    earTemperatureMesureResult.onDevicePoweroff();
                }
            });

        }
    }

    public void setEarTemperatureMeasureCallback(EarTemperatureMesureResult earTemperatureMesureResult) {
        this.earTemperatureMesureResult=earTemperatureMesureResult;
    }
}
